﻿#region Namespaces

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

#endregion

namespace Vibstudio.X4NET.Xml.Conversion
{
    public class CsvConversionConfig
    {
        #region Ctor(s)

        public CsvConversionConfig()
        {
            _settings = new List<CsvConversionSetting>();
        }

        #endregion

        #region Helpers

        private void EnsureCsvHeaderRow()
        {
            if (string.IsNullOrEmpty(_csvHeader))
            {
                StringBuilder sb = new StringBuilder();
                foreach (CsvConversionSetting conversionSetting in Settings)
                {
                    sb.AppendFormat("{0}{1}", conversionSetting.CsvColumnName, Defaults.CsvSeparator);
                }
                sb.Length--;

                CsvHeaderRow = sb.ToString();
            }
        }

        #endregion

        #region Fields

        private string _csvHeader;

        private List<CsvConversionSetting> _settings;

        #endregion

        #region Properties

        public string XsdPath { get; set; }

        public XmlDocument XmlTemplate { get; set; }

        public string CsvHeaderRow
        {
            get
            {
                EnsureCsvHeaderRow();
                return _csvHeader;
            }
            set { _csvHeader = value; }
        }

        public List<CsvConversionSetting> Settings
        {
            get { return _settings.OrderBy(x => x.XPath.Index).ToList(); }
            set { _settings = value; }
        }

        #endregion
    }
}